
// フォロー
function initializeFriends(params) {
	$.mobile.showPageLoadingMsg();
	api.getFriends(params, function(data) {
		$.mobile.hidePageLoadingMsg();
		var old_data = data;
		api.getFriendsids({user_id : gUser.User.id}, function(data) {
			//console.log(old_data);
			//console.log(data);
		var last, count = 0;
			var user = null;
			if(friends.usersData.length != 0) {
				last = friends.usersData[friends.usersData.length - 1];
			}
			for(index in old_data.users) {
				user = old_data.users[index];
				// 重複を避ける
				if(last == null || last.id != user.id) {
					count++;
					var flag = false;
					for(index2 in data.ids) {
						if(user.id == data.ids[index2]) {
							flag = true;
							break;
						}
					}
					friends.append(user, flag);
				}
			}
			//フォローイベント
			$('.follow-button').click(followEvent);
			//アンフォローイベント
			$('.unfollow-button').click(unfollowEvent);

			if(count > 0 && old_data.next_cursor !="0") {
				//次を見る奴
				friends.appendNextTweet();
				//次を見る
				$('#myfriends div:last').click(function() {
					$(this).remove();
					params['cursor'] = old_data.next_cursor;
					initializeFriends(params);
				});
			}

		});
	});
}

// フォロワー
function initializeFollowers(params) {

	$.mobile.showPageLoadingMsg();
	api.getFollowers(params, function(data) {
		$.mobile.hidePageLoadingMsg();
		var old_data = data;
		api.getFriendsids({user_id : gUser.User.id}, function(data) {
			//console.log(old_data);
			//console.log(data);
			var last, count = 0;
			var user = null;
			if(followers.usersData.length != 0) {
				last = followers.usersData[followers.usersData.length - 1];
			}
			for(index in old_data.users) {
				user = old_data.users[index];
				// 重複を避ける
				if(last == null || last.id != user.id) {
					count++;
					var flag = false;
					for(index2 in data.ids) {
						if(user.id == data.ids[index2]) {
							flag = true;
							break;
						}
					}
					followers.append(user, flag);
				}
			}
			//フォローイベント
			$('.follow-button').click(followEvent);
			//アンフォローイベント
			$('.unfollow-button').click(unfollowEvent);

			if(count > 0 && old_data.next_cursor !="0") {
				//次を見る奴
				followers.appendNextTweet();
				//次を見る
				$('#myfollowers div:last').click(function() {
					$(this).remove();
					params['cursor'] = old_data.next_cursor;
					initializeFollowers(params);
				});
			}

		});
	});
}